package com.ruoyi.zhjd.exp.modules.laboratorydevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.zhjd.common.enums.BizCode;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.dto.LaboratoryDeviceDto;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.entity.LaboratoryDeviceEntity;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.mapper.LaboratoryDeviceMapper;
import com.ruoyi.zhjd.exp.modules.laboratorydevice.service.LaboratoryDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName: LaboratoryDeviceServiceImpl
 * @Description: 实验室设备业务接口实现
 * @author linhuawei
 * @since 2021-05-17
 */
@Service
public class LaboratoryDeviceServiceImpl extends ServiceImpl<LaboratoryDeviceMapper, LaboratoryDeviceEntity>
        implements LaboratoryDeviceService {

//    @Autowired
//    private BaseServiceComponent baseServiceComponent;

    @Override
//    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<LaboratoryDeviceDto> page(Map<String, Object> params) {
        IPage<LaboratoryDeviceDto> page = null;
        return baseMapper.queryDto(page, params);
    }

    @Override
    public boolean save(LaboratoryDeviceEntity entity) {
        if (null == entity.getNum()) {
            entity.setNum(1);
        }
        return super.save(entity);
    }

    @Override
    public boolean updateById(LaboratoryDeviceEntity entity) {
        if (null == entity.getNum()) {
            entity.setNum(1);
        }
        return super.updateById(entity);
    }

    @Override
    public void bindCode(String id, String code) {
        // 判断设备是否存在
        LaboratoryDeviceEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            throw new ServiceException( "试验设备不存在");
        }
        if (ObjectUtils.isNotEmpty(entity.getCode())) {
            throw new ServiceException( "当前试验设备已经绑定终端");
        }
        if (null != entity.getNum() && 1 != entity.getNum()) {
            throw new ServiceException( "设备数量不为1.不可绑定终端");
        }

        // 判断设备编号是否存在
        code = entity.getBaseCode() + "D" + code;
        code.toUpperCase();
        QueryWrapper<LaboratoryDeviceEntity> queryWrapper = new QueryWrapper<LaboratoryDeviceEntity>();
        queryWrapper.eq("code", code);
        Long count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            throw new ServiceException( "终端编号[" + code + "]已经存在");
        }

        // 执行更新操作
        UpdateWrapper<LaboratoryDeviceEntity> updateWrapper = new UpdateWrapper<LaboratoryDeviceEntity>();
        updateWrapper.set("code", code);
        updateWrapper.eq("id", id);
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void unBindCode(String id) {
        // 判断设备是否存在
        LaboratoryDeviceEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            throw new ServiceException( "试验设备不存在");
        }
        if (ObjectUtils.isEmpty(entity.getCode())) {
            throw new ServiceException( "当前试验设备未绑定终端");
        }

        // 执行更新操作
        UpdateWrapper<LaboratoryDeviceEntity> updateWrapper = new UpdateWrapper<LaboratoryDeviceEntity>();
        updateWrapper.set("code", null);
        updateWrapper.eq("id", id);
        baseMapper.update(null, updateWrapper);
    }

}
